package com.ahwei.controller;

import com.ahwei.context.AppContext;
import com.ahwei.dao.function.FunctionDao;
import com.ahwei.dao.user.BrowseDao;
import com.ahwei.pojo.*;
import com.ahwei.pojo.model.PageResult;
import com.ahwei.service.BrowseService;
import com.ahwei.service.impl.BrowseServiceImpl;
import com.ahwei.service.impl.VideoServiceImpl;
import com.ahwei.util.MyBatisUtils;
import com.ahwei.util.anno.Controller;
import com.ahwei.util.anno.FunctionMapping;
import com.ahwei.util.anno.RequestMapping;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-15 15:37:49
 * @description: 这是一个Java文件！
 */
@Controller
public class RoleController {


    @FunctionMapping("根据角色获取所有功能")
    @RequestMapping("/queryAllFunctionByRole")
    public List<Map<String,Object>> queryAllFunctionByRole(String roleId) {
        Map<String,Object> map = new HashMap<>();
        map.put("roleId", roleId);

        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        FunctionDao functionDao = sqlSession.getMapper(FunctionDao.class);
        return functionDao.queryAllFunction(map);
    }

    @FunctionMapping("删除角色权限")
    @RequestMapping("/deleteRoleFunction")
    public Boolean deleteRoleFunction(int funId, int roleId){
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        FunctionDao functionDao = sqlSession.getMapper(FunctionDao.class);
        return functionDao.deleteRoleFunction(funId,roleId) > 0;
    }

    @FunctionMapping("获取所有角色")
    @RequestMapping("/getAllRole")
    public PageResult<Role> getAllRole(String roleId, String roleName, Integer pageNo) {
        //每页条数乘以当前页数减一
        int getData = AppContext.PAGE_COUNT * (pageNo - 1);
        //sql参数
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("roleName", roleName);

        if (pageNo > 0) {
            map.put("getData", getData);
            map.put("pageCount", AppContext.PAGE_COUNT);
        }

        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        FunctionDao functionDao = sqlSession.getMapper(FunctionDao.class);

        PageResult<Role> pageResult = new PageResult<>();
        //获取集合
        List<Role> allRole = functionDao.getAllRole(map);
        pageResult.setPageNo(pageNo);
        pageResult.setData(allRole);
        pageResult.setMaxPageNo(functionDao.findAllRoleCount(map));

        return pageResult;
    }

    private User User(HttpSession session) {
        return (User) session.getAttribute("currentUser");
    }

    private BrowseService browseService() {
        return new BrowseServiceImpl();
    }
}
